# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/12 22:24
# @Author   : Robot-Zsj
"""
description:

i:
City1=1.1.1.1,1.1.1.2;City1=1.1.1.11,1.1.1.16;City2=3.3.3.4,4.4.4.4;City3=2.2.2.2,6.6.6.6
1.1.1.15,3.3.3.5,2.2.2.3

o:
City1,City2,City3
"""
import pdb
import re
import sys


def ip2dec(ipStr):
    res = 0

    blocks = ipStr.split(".")
    for block in blocks:
        res = int(block) | (res << 8)

    return res


class Range:
    def __init__(self, city, starIpStr, endIpStr):
        self.city = city

        self.startIp = ip2dec(starIpStr)
        self.endIp = ip2dec(endIpStr)
        self.ipLen = self.endIp - self.startIp + 1


cities = input().split(";")
queryIps = input().split(",")

ranges = []

for s in cities:
    ranges.append(Range(*(re.split(r"[=,]", s))))

ans = []

for ip in queryIps:
    ipDec = ip2dec(ip)
    best_city = ""

    minLen = sys.maxsize

    for ran in ranges:
        if ran.endIp >= ipDec >= ran.startIp:

            if minLen > ran.ipLen or (minLen == ran.ipLen and best_city < ran.city):
                best_city = ran.city
                minLen = ran.ipLen

    ans.append(best_city)

print(",".join(ans))
